
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<cstring>
#include "sriTCPCLient.h"

using namespace std;
sriTCPClient::sriTCPClient(){
    m_sockfd=0;
}

sriTCPClient::~sriTCPClient(){
    close(m_sockfd);
}

bool sriTCPClient::ConnectToHost(const char *serverip, const int port){
    m_sockfd=socket(AF_INET,SOCK_STREAM,0);
    struct hostent* h;
    if((h=gethostbyname(serverip))==0){
        close(m_sockfd);
        m_sockfd=0;
        return false;
    }
    struct sockaddr_in serveraddr;
    serveraddr.sin_family=AF_INET;
    serveraddr.sin_port=htons(port);
    memcpy(&serveraddr.sin_addr,h->h_addr,h->h_length);
    if(connect(m_sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr))){
        close(m_sockfd);
        m_sockfd=0;
        return false;
    }

    return true;
}

int sriTCPClient::Send(const void* buffer, const int bufferlen){
    return send(m_sockfd,buffer,bufferlen,0);
}

int sriTCPClient::Recv(void *buffer, const int bufferlen){
    return recv(m_sockfd,buffer,bufferlen,0);
}














